import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:osg/controllers/http.dart';
import 'package:osg/controllers/my_method.dart';
import 'package:url_launcher/url_launcher.dart';

import '/kong.dart' if (dart.library.io) 'package:osg/middleware/im_manager.dart';


class LoginType extends StatefulWidget {
  LoginType({super.key});

  @override
  State<LoginType> createState() => _SettingPageState();
}

class _SettingPageState extends State<LoginType> {
// class LoginType extends StatelessWidget {
  final RxBool _isLogin = true.obs;
  bool _obscureText = true;
  final FocusNode _focusNode = FocusNode();
  RxInt codetext = 60.obs;
  RxBool butdisabled = false.obs;

  RxBool valgroup = true.obs;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

  }


  final TextEditingController phone =
      TextEditingController(text:Get.find<MyMethod>().myget('phone'));
  final TextEditingController password = TextEditingController(text: Get.find<MyMethod>().myget('pws'));

  final TextEditingController code = TextEditingController();

  @override
  void dispose() {
    code.dispose();
    phone.dispose();
    password.dispose();
 _focusNode.dispose();
    super.dispose();
  }
 

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          backgroundColor: Colors.black12,
        ),
        extendBodyBehindAppBar: true,
        body: 
        GestureDetector(
          onTap: (){
            FocusScope.of(context).unfocus();
          },
          child: Column(
          children: [
            SafeArea(
              top: false,
              child: Image.asset(
                'static/images/dengli.png',
                fit: BoxFit.fitWidth,
              ),
            ),
            Container(
              height: 30,
              transform: Matrix4.translationValues(0.0, -15.0, 0.0),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(30),
                  color: Color(0XFFFFFFFF)),
            ),
            Expanded(
              child: ListView(
                children: [
                  Column(
                    children: [
                      Container(
                        transform: Matrix4.translationValues(0.0, -40.0, 0.0),
                        child: 
                        GestureDetector(
                          onTap: (){
                            Get.toNamed('/login/splash_screen');
                          },
                          child: Image.asset(
                          'static/images/open.png',
                          fit: BoxFit.fitWidth,
                          width: 160,
                        ),
                        )
                        ,
                      ),
                      // Obx(
                      //   () => _isLogin.value
                      //       ?
                      Container(
                        margin:
                            EdgeInsets.symmetric(horizontal: 30, vertical: 10),
                        decoration: BoxDecoration(
                            border: Border(
                                bottom: BorderSide(color: Color(0XFFDDDDDD)))),
                        child: Row(
                          children: [
                            SizedBox(
                              width: 15,
                            ),
                            Image.asset(
                              'static/images/guoqi.png',
                              fit: BoxFit.fitWidth,
                              width: 30,
                            ),
                            SizedBox(
                              width: 5,
                            ),
                            Text('+86', style: TextStyle(fontSize: 14)),
                            Expanded(
                               child:
                              TextField(
                                focusNode: _focusNode,
                              controller: phone,
                              style: TextStyle(fontSize: 14),
                              decoration: InputDecoration(
                                isCollapsed: true, //设置高度
                                contentPadding: EdgeInsets.all(15),
                                hintStyle: TextStyle(color: Color(0xff999999)),
                                border: InputBorder.none, // 隐藏输入框默认边框
                                hintText: '请输入手机号', // 提示文本
                              ),
                            )
                                )
                          ],
                        ),
                      )
                      // : Container(
                      //     margin: EdgeInsets.symmetric(horizontal: 30, vertical: 10),
                      //     decoration: BoxDecoration(
                      //         border: Border(
                      //             bottom: BorderSide(color: Color(0XFFDDDDDD)))),
                      //     child: TextField(
                      //       style: TextStyle(fontSize: 14),
                      //       decoration: InputDecoration(
                      //         isCollapsed: true, //设置高度
                      //         contentPadding: EdgeInsets.all(15),
                      //         hintStyle: TextStyle(color: Color(0xff999999)),
                      //         border: InputBorder.none, // 隐藏输入框默认边框
                      //         hintText: '请输入电子邮箱地址', // 提示文本
                      //       ),
                      //     )),
                      // ),
                      ,
                      Obx(() => _isLogin.value
                          ? Container(
                              margin: EdgeInsets.symmetric(horizontal: 30),
                              decoration: BoxDecoration(
                                  border: Border(
                                      bottom: BorderSide(
                                          color: Color(0XFFDDDDDD)))),
                              child: TextField(
                                obscureText: true,
                                controller: password,
                                style: TextStyle(fontSize: 14),
                                decoration: InputDecoration(
                                  // suffixIcon: IconButton(
                                  //   // 使用IconButton作为后缀图标按钮
                                  //   icon: Icon(
                                  //     size: 15,
                                  //     _obscureText
                                  //         ? Icons.visibility_off
                                  //         : Icons.visibility, // 根据_obscureText的值切换图标
                                  //   ),
                                  //   onPressed: () {}, // 点击图标时调用_toggleVisibility方法
                                  // ),
                                  isCollapsed: true, //设置高度
                                  contentPadding: EdgeInsets.all(15),
                                  hintStyle:
                                      TextStyle(color: Color(0xff999999)),
                                  border: InputBorder.none, // 隐藏输入框默认边框
                                  hintText: '请输入密码', // 提示文本
                                ),
                              ))
                          : Container(
                              margin: EdgeInsets.symmetric(
                                  horizontal: 30, vertical: 10),
                              decoration: BoxDecoration(
                                  border: Border(
                                      bottom: BorderSide(
                                          color: Color(0XFFDDDDDD)))),
                              child: Row(
                                children: [
                                  Expanded(
                                    child: TextField(
                                      controller: code,
                                      style: TextStyle(fontSize: 14),
                                      decoration: InputDecoration(
                                        isCollapsed: true, //设置高度
                                        contentPadding: EdgeInsets.all(15),
                                        hintStyle:
                                            TextStyle(color: Color(0xff999999)),
                                        border: InputBorder.none, // 隐藏输入框默认边框
                                        hintText: '请输入验证码 (新用户登录即注册)', // 提示文本
                                      ),
                                    ),
                                  ),
                                  GestureDetector(
                                    onTap: () {
                                      if (phone.text.isNotEmpty) {
                                        if (butdisabled.value) {
                                          return;
                                        }
                                        Get.find<HTTPS>()
                                            .postData('/phone-code', {
                                          "phone": phone.text,
                                          "value": "login",
                                        }).then((res) {
                                          if (res != null) {
                                            Get.find<HTTPS>().showTost('发送成功');
                                            butdisabled.value = true;
                                            codetext.value = 60;

                                            Timer.periodic(Duration(seconds: 1),
                                                (timer) {
                                              codetext.value--;
                                              if (codetext.value < 1) {
                                                codetext.value = 1;
                                                timer.cancel();
                                                butdisabled.value = false;
                                              }

                                              // if(timer.tick == 60) timer.cancel(); // 执行5次后停止
                                            });
                                          }
                                        });
                                      } else {
                                        Get.find<HTTPS>().showTost('输入手机号');
                                      }
                                    },
                                    child: Obx(() => butdisabled.value
                                        ? Text('已发送$codetext',
                                            style: TextStyle(
                                                fontSize: 12,
                                                color: Color(0XFF0A72D9)))
                                        : Text('发送验证码',
                                            style: TextStyle(
                                                fontSize: 12,
                                                color: Color(0XFF0A72D9)))),
                                  ),
                                ],
                              ))),

                      Container(
                          margin:
                              EdgeInsets.only(right: 30, top: 15, bottom: 30),
                          alignment: Alignment.centerRight,
                          child: GestureDetector(
                            onTap: () {
                              _isLogin.value = !_isLogin.value;
                              code.text = '';
                              password.text = '';
                            },
                            child: Obx(() => _isLogin.value
                                ? Text('验证码登录',
                                    style: TextStyle(
                                        fontSize: 12, color: Color(0XFF0A72D9)))
                                : Text('密码登录',
                                    style: TextStyle(
                                        fontSize: 12,
                                        color: Color(0XFF0A72D9)))),
                          )),
                      GestureDetector(

                        onTap: () async{


    //                      final uur=  Uri.parse("https://www.baidu.com/s?ie=UTF-8&wd=2");

    // // launchUrl(uur);

    //                 if (await canLaunchUrl(uur)) {
    //                   Get.find<HTTPS>().showTost('取消成功');
    //                     await launchUrl(uur);
    //                   }else{
    //                   Get.find<HTTPS>().showTost('666');

    //                   }

      



                          if (!phone.text.isNotEmpty) {
                            Get.find<HTTPS>().showTost('输入手机号');
                            return;
                          }
                          if (_isLogin.value) {
                            if (!password.text.isNotEmpty) {
                              Get.find<HTTPS>().showTost('输入密码');
                              return;
                            }
                          } else {
                            if (!code.text.isNotEmpty) {
                              Get.find<HTTPS>().showTost('请输入验证码');
                              return;
                            }
                          }

                        Get.find<HTTPS>().loading();
                          Get.find<HTTPS>().nopostData('/login', {
                            'phone': phone.text,
                            'password': password.text,
                            'phone_code': code.text
                          }).then((res) {
   
                            if (res != null) {
                            
                              Get.find<MyMethod>().myset('token', res['token']);
                              Get.find<MyMethod>().myset('phone', phone.text);
                              Get.find<MyMethod>().myset('pws', password.text);
                              
                              Get.find<HTTPS>().nogetData('/userInfo', {}).then((res) {
                                Get.find<MyMethod>().myset('user', res);

                                  Get.find<HTTPS>().nogetData('/tim/getSin', {
                                  }).then((ress){
                                    if(ress!=null){

                                    if(kIsWeb){
                                        Get.offNamed('/');
                                    }else{
                                       ImManager().login(ress['sig'], ress['UserID'],res['name'],faceUrl:res['headimg']).then((val){
                                        if(val){
                                          print(111112323);
                                          print(val);

                                            Get.offNamed('/');
                                        }else{
                                           Get.find<HTTPS>().showTost('IM登录失败');
                                        }
                                      });
                                    }
                                    }
                                  });
                              });
                            }
                            else{
                              Get.back();
                               
                            }

                          });
                        },
                        child: Container(
                            width: 300,
                            height: 50,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular((50)),
                              gradient: LinearGradient(
                                colors: [
                                  Color(0xff2189E7),
                                  Color(0xffBF67F8),
                                ], // 定义颜色列表
                                begin: Alignment.topLeft, // 渐变开始的位置
                                end: Alignment.bottomRight, // 渐变结束的位置
                              ),
                            ),
                            child: Text('登 录',
                                style: TextStyle(
                                    fontSize: 16, color: Color(0XFFFFFFFF)))),
                      ),
                      SizedBox(
                        height: 20,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          GestureDetector(
                            onTap: () {
                              Get.toNamed('/login/register_tow');
                            },
                            child: Text('注册新用户',
                                style: TextStyle(
                                    fontSize: 12, color: Color(0XFF999999))),
                          ),
                          GestureDetector(
                            onTap: () {
                              Get.toNamed('/login/get_password');
                            },
                            child: Text('忘记密码？',
                                style: TextStyle(
                                    fontSize: 12, color: Color(0XFF999999))),
                          ),
                        ],
                      ),
                      // Container(
                      //   margin: EdgeInsets.only(left: 100, right: 100, top: 50),
                      //   child: Row(
                      //     mainAxisAlignment: MainAxisAlignment.spaceAround,
                      //     children: [
                      //       Image.asset(
                      //         'static/images/weixin.png',
                      //         width: 40,
                      //         fit: BoxFit.fitWidth,
                      //       ),
                      //       Image.asset(
                      //         'static/images/pingguo.png',
                      //         width: 40,
                      //         fit: BoxFit.fitWidth,
                      //       ),
                      //     ],
                      //   ),
                      // ),
                    ],
                  ),
                ],
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                GestureDetector(
                  onTap: () {
                    valgroup.value = !valgroup.value;
                  },
                  child: Container(
                      width: 12,
                      height: 12,
                      margin: EdgeInsets.only(right: 3),
                      child: Obx(() => Image.asset(
                            valgroup.value
                                ? 'static/web/shous.png'
                                : 'static/web/shou.png',
                            width: 12,
                            height: 12,
                            fit: BoxFit.fitWidth,
                          ))),
                ),
                GestureDetector(
                  onTap: () {
                    valgroup.value = !valgroup.value;
                  },
                  child: Text('已阅读并同意', style: TextStyle(fontSize: 11)),
                ),
                GestureDetector(
                  onTap: () {
                    Get.toNamed('/agreement?type=1', arguments: {'type': '1'});
                  },
                  child: Text('《用户服务协议》',
                      style: TextStyle(fontSize: 11, color: Color(0XFF0A72D9))),
                ),
                Text('与', style: TextStyle(fontSize: 11)),
                GestureDetector(
                  onTap: () {
                    Get.toNamed('/agreement?type=2', arguments: {'type': '1'});
                  },
                  child: Text('《隐私权政策》',
                      style: TextStyle(fontSize: 11, color: Color(0XFF0A72D9))),
                ),
              ],
            ),
            SizedBox(
              height: 15,
            )
          ],
        ),
        )
        
        );
  }
}
